<template>
	<view class="add bg_color pdd_bottom2">
		<view v-if="!isEnter">
			<view class="tit">
				<view class="text">主要信息</view>
				<view class="xing">*</view>
			</view>
			<Form ref="formRef" :formList="formList" :formData="formData"></Form>
			<view class="tit">
				<view class="text">单位地址</view>
				<view class="xing">*</view>
			</view>
			<Form ref="formRef1" :formList="formList1" :formData="formData"></Form>
			<view class="tit">
				<view class="text">单位注册地址</view>
				<view class="xing">*</view>
			</view>
			<Form ref="formRef2" :formList="formList2" :formData="formData"></Form>
			<view class="tit">
				<view class="text">基本信息</view>
			</view>
			<Form ref="formRef3" :formList="formList6" :formData="formData"></Form>
			<view class="tit">
				<view class="text">负责人/法定代表人</view>
				<view class="xing">*</view>
			</view>
			<Form ref="formRef4" :formList="formList4" :formData="formData"></Form>
			<view class="tit">
				<view class="text">保卫负责人信息</view>
			</view>
			<Form ref="formRef5" :formList="formList5" :formData="formData"></Form>
			<view class="butt font28" @click="addItem">提交</view>
		</view>
		<SubmitState v-else :success="success" @successReturn="fanhui" @failReturn="isEnter = false"></SubmitState>
	</view>
	<FuJinSubmitModal :deleteShow="deleteShow1" @confirm="confirm1" @close="deleteShow1 = false"></FuJinSubmitModal>
</template>

<script setup>
import Form from 'com/Form.vue';
import SubmitState from 'com/modal/SubmitState.vue';
import FuJinSubmitModal from 'com/modal/FuJinSubmitModal.vue';
import changeData from 'utils/changeData.js';
import Api from 'api';
import { formLinShi, formList, formList1, formList2, formList3, formList4, formList5 } from './formListData';
import { usersStore } from 'store/users.js';
const api = new Api
const store = usersStore();
const id = ref('');
const formRef = ref(null);
const formRef1 = ref(null);
const formRef2 = ref(null);
const formRef3 = ref(null);
const formRef4 = ref(null);
const isEnter = ref(false);
const deleteShow1 = ref(false);
const success = ref('');
const formListxinxi = ref([...formList3]);
import { dictStore } from 'store/dicts.js';
const store1 = dictStore();
onLoad(async (query) => {
	const res = await store1.getDict('urban_type');
	formListxinxi.value.find((item) => item.prop == 'unitType').columns = res;
	const res1 = await store1.getDict('business_model');
	formListxinxi.value.find((item) => item.prop == 'businessModel').columns = res1;
	if (query.itemData) {
		const res = JSON.parse(query.itemData);
		const find = formListxinxi.value.find((item) => item.prop == res.prop);
		formData.value = JSON.parse(res.formData);
		formData.value[res.prop] = res.value;
		find.columns = [{ name: res.name, value: res.value }];
	}
	if (query.id) {
		uni.setNavigationBarTitle({
			title: '编辑实有单位'
		});
		const findList = formListxinxi.value.filter((item) => item.code);
		findList.forEach(async (item) => {
			const res = await store1.getDict(item.code);
			item.columns = res;
		});
		id.value = query.id;
		const res = await api.queryById('baseUnit',{ id: query.id });
		formData.value = res;
	} else {
		uni.setNavigationBarTitle({
			title: '新增实有单位'
		});
	}
});
const formList6 = ref([...formListxinxi.value]);
const formData = ref({
	companyName: '',
	socialCreditCode: '',
	companyProvince: '',
	companyPlace: '',
	registerProvince: '',
	registerPlace: '',
	businessName: '',
	enName: '',
	enAbbreviation: '',
	keyUnit: '',
	firefightingUnits: '',
	antiUnit: '',
	supervisoryUnit: '',
	unitType: '',
	economicType: '',
	industryCategory: '',
	openingDate: '',
	closingDate: '',
	businessArea: '',
	businessModel: '',
	businessLicenseValid: '',
	phone: '',
	businessScope: '',
	businessConcurrentScope: '',
	legalName: '',
	legalPhone: '',
	legalCard: '',
	chargePersonName: '',
	chargePersonPhone: '',
	chargePersonCard: ''
});
watch(
	() => formData.value.keyUnit,
	(newVal) => {
		if (newVal == 1) formList6.value.splice(4, 0, ...formLinShi);
		else {
			formData.value.firefightingUnits = ''
			formData.value.antiUnit = ''
			formList6.value = [...formList3];}
	}
);
watch(()=>formData.value.openingDate,(newVal)=>{
	const find = formList6.value.find(item =>item.prop == 'closingDate')
	find.disabled = false
	find.placeholder = '请选择时间'
	find.minDate = newVal
})
watch(()=>formData.value.closingDate,(newVal)=>{
	const find = formList6.value.find(item =>item.prop == 'openingDate')
	if(newVal){
		find.maxDate = newVal
	}
})
const fanhui = () => {
	uni.navigateBack()
};
const addItem = () => {
	const submitPromises = [formRef.value, formRef1.value, formRef2.value, formRef3.value, formRef4.value].map((form) => {
		return new Promise((resolve) => {
			form.submit(() => {
				resolve();
			});
		});
	});
	Promise.all(submitPromises).then(() => {
		store.isFuJing() ? (deleteShow1.value = true) : end();
	});
};
const confirm1 = () => {
	end();
};
const end = async () => {
	deleteShow1.value = false;
	let res;
	if (!store.users?.assress || store.users.assress.length == 0) {
		uni.redirectTo({
			url: '/pages/index/index'
		});
		return
	}
	const data = store.users.assress;
	const targetArea = changeData.findArea(unref(data), formData.value.companyProvince.split('-'));
	formData.value.provinceId = targetArea 
	if (id.value) {
		res = await api.edit('baseUnit', unref(formData));
	} else {
		res = await api.add('baseUnit', unref(formData));
	}
	success.value = res;
	isEnter.value = true;
};
</script>

<style lang="scss" scoped></style>